<!DOCTYPE html>
<html lang="zh">
{% extends "base.html" %}

{% block title %}收费统计{% endblock %}

{% block styles %}
<!-- 添加 Select2 的 CSS -->
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css" rel="stylesheet" />
<style>
    .select2-container {
        width: 100% !important;
    }
</style>
{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2>收费统计</h2>
    <div>
        <button class="btn btn-secondary me-2" type="button" data-bs-toggle="collapse" data-bs-target="#filterForm">
            过滤条件 <i class="bi bi-funnel"></i>
        </button>
        <a href="{{ url_for('export_payment_summary') }}?{{ request.query_string.decode() }}" class="btn btn-success">
            <i class="bi bi-download me-1"></i>导出
        </a>
    </div>
</div>

<!-- 添加过滤表单（可折叠） -->
<div class="collapse mb-4" id="filterForm">
    <div class="card card-body">
        <form method="GET" class="row g-3">
            <div class="col-md-3">
                <label for="grade" class="form-label">年级</label>
                <select class="form-control" id="grade" name="grade">
                    <option value="">全部</option>
                    {% for g in grades %}
                    <option value="{{ g.name }}" {% if g.name == selected_grade %}selected{% endif %}>{{ g.name }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="col-md-3">
                <label for="year" class="form-label">年度</label>
                <select class="form-control" id="year" name="year">
                    <option value="">全部</option>
                    {% for y in years %}
                    <option value="{{ y }}" {% if y == selected_year %}selected{% endif %}>{{ y }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="col-md-3">
                <label for="semester" class="form-label">学期</label>
                <select class="form-control" id="semester" name="semester">
                    <option value="">全部</option>
                    {% for s in semesters %}
                    <option value="{{ s.name }}" {% if s.name == selected_semester %}selected{% endif %}>{{ s.name }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="col-md-3">
                <label for="student" class="form-label">学生</label>
                <select class="form-control" id="student" name="student">
                    <option value="">全部</option>
                    {% for s in students %}
                    <option value="{{ s.name }}" {% if s.name == selected_student %}selected{% endif %}>{{ s.name }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="col-md-3">
                <label for="start_date" class="form-label">起始日期</label>
                <input type="date" class="form-control" id="start_date" name="start_date" 
                       value="{{ selected_start_date or min_date.strftime('%Y-%m-%d') }}">
            </div>
            <div class="col-md-3">
                <label for="end_date" class="form-label">结束日期</label>
                <input type="date" class="form-control" id="end_date" name="end_date" 
                       value="{{ selected_end_date or max_date.strftime('%Y-%m-%d') }}">
            </div>
            <div class="col-md-6">
                <div class="form-check mb-3">
                    <input class="form-check-input" type="checkbox" id="hide_zero" name="hide_zero" value="1" {% if hide_zero %}checked{% endif %}>
                    <label class="form-check-label" for="hide_zero">
                        隐藏无收费记录
                    </label>
                </div>
                <div>
                    <button type="submit" class="btn btn-primary">查询</button>
                    <a href="{{ url_for('payment_summary') }}" class="btn btn-secondary ms-2">重置</a>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="table-responsive">
    <table class="table table-striped">
        <thead>
            <tr>
                <th>注册编号</th>
                <th>学生</th>
                <th>班级</th>
                {% for project in projects %}
                <th>{{ project.name }}</th>
                {% endfor %}
                <th>合计</th>
            </tr>
        </thead>
        <tbody>
            {% for summary in summaries %}
            <tr>
                <td>{{ summary.registration_id }}</td>
                <td>{{ summary.student }}</td>
                <td>{{ summary.class_name }}</td>
                {% for project in projects %}
                <td>{{ "%.2f"|format(summary.fees.get(project.name, 0)) }}</td>
                {% endfor %}
                <td>{{ "%.2f"|format(summary.total) }}</td>
            </tr>
            {% endfor %}
            <tr class="table-info fw-bold">
                <td colspan="3" class="text-end">合计：</td>
                {% for project in projects %}
                {% set project_total = summaries|map(attribute='fees')|map(attribute=project.name)|select('number')|sum %}
                <td>{{ "%.2f"|format(project_total) }}</td>
                {% endfor %}
                <td>{{ "%.2f"|format(total_amount) }}</td>
            </tr>
        </tbody>
    </table>
</div>

<div class="mt-4">
    <h3>统计信息</h3>
    <div class="row">
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">总收费金额</h5>
                    <p class="card-text">{{ "%.2f"|format(total_amount) }}</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">学生人数</h5>
                    <p class="card-text">{{ student_count }}</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">人均收费</h5>
                    <p class="card-text">{{ "%.2f"|format(avg_amount) }}</p>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<!-- 添加 Select2 的 JS -->
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/i18n/zh-CN.js"></script>
<script>
$(document).ready(function() {
    // 初始化学生选择框
    $('#student').select2({
        theme: 'bootstrap-5',
        language: 'zh-CN',
        placeholder: '请选择学生',
        allowClear: true
    });
    
    // 如果有过滤条件被设置，自动展开过滤表单
    const urlParams = new URLSearchParams(window.location.search);
    if (urlParams.toString() && !urlParams.has('_')) {
        document.getElementById('filterForm').classList.add('show');
    }
});
</script>
{% endblock %} 